package br.com.eproducao.model.persistence;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import br.com.eproducao.model.entity.produto.Risco;

@Repository
public class RiscoRepository extends BaseRepository<Risco>
{
	@Autowired
	public void init(SessionFactory factory) {
	    setSessionFactory(factory);
	}
	
	public List<Risco> buscarRiscoPorDescricao(String descricao)
	{
		DetachedCriteria criteria = DetachedCriteria.forClass( Risco.class );
		criteria.add( Restrictions.like( "descRisco", descricao, MatchMode.ANYWHERE ) );
		return list( criteria );
	}
	
	public boolean verificarDuplicado(String desc)
	{
		DetachedCriteria criteira = DetachedCriteria.forClass( Risco.class );
		criteira.add( Restrictions.eq( "descRisco", desc ) );
		return (list( criteira ).size() > 0);
	}
	
	public boolean verificarDuplicado(String desc, long id)
	{
		DetachedCriteria criteira = DetachedCriteria.forClass( Risco.class );
		criteira.add( Restrictions.eq( "descRisco", desc ) );
		criteira.add( Restrictions.ne( "codRisco", id ) );
		return (list( criteira ).size() > 0);
	}
}
